﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace HotelManagement.PL
{
    public partial class MainForm : Form
    {
		private BaseForm m_BaseForm = new BaseForm();

        public MainForm()
        {
            InitializeComponent();
        }



        private void SearchToolStripMenuItem_Click(object sender, EventArgs e)
        {
			//ShowChildForm(typeof(SearchForm).ToString());
			m_BaseForm.SwitchUserControl(HotelManagement.Common.OperationType.TradeMng);
        }

        #region ShowChildForm
        /// <summary>
        /// 显示子窗体
        /// </summary>
        /// <param name="ChildTypeString">子窗体类型</param>
        private void ShowChildForm(string ChildTypeString)
        {
            Form frmMyChild = null;
            try
            {
                if (!ContainMDIChild(ChildTypeString))
                {
                    // Get current process assembly
                    Assembly assembly = Assembly.GetExecutingAssembly();

                    // Create data type using type string
                    Type typForm = assembly.GetType(ChildTypeString);

                    // Create object using type's "InvokeMember" method
                    Object obj = typForm.InvokeMember(
                        null,
                        BindingFlags.DeclaredOnly |
                        BindingFlags.Public | BindingFlags.NonPublic |
                        BindingFlags.Instance | BindingFlags.CreateInstance,
                        null,
                        null,
                        null);

                    // Show child form 
                    if (obj != null)
                    {
                        frmMyChild = obj as Form;
                        frmMyChild.MdiParent = this;

                        frmMyChild.WindowState = FormWindowState.Maximized;
                        frmMyChild.Show();
                        frmMyChild.Focus();
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        #endregion ShowChildForm

        #region ContainMDIChild
        /// <summary>
        /// 判断是否已经显示子窗体
        /// </summary>
        /// <param name="ChildTypeString">子窗体类型</param>
        private bool ContainMDIChild(string ChildTypeString)
        {
            Form frmMDIChild = null;
            foreach (Form f in this.MdiChildren)
            {
                //如果已经打开窗口，则显示，否则关闭已打开窗口，打开新窗口
                if (f.GetType().ToString() == ChildTypeString)
                {
                    // found it 
                    frmMDIChild = f;
                }
                else
                {
                    f.Close();
                }
            }
            // Show the exist form
            if (frmMDIChild != null)
            {
                frmMDIChild.TopMost = true;
                frmMDIChild.WindowState = FormWindowState.Maximized;
                frmMDIChild.Show();
                frmMDIChild.Focus();
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion ContainMDIChild


        private void AddGoodsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowChildForm(typeof(GoodsEditForm).ToString());
        }

        private void ManageToolStripMenuItem_Click(object sender, EventArgs e)
        {
			//ShowChildForm(typeof(SearchGoodsTypeForm).ToString());
			m_BaseForm.SwitchUserControl(HotelManagement.Common.OperationType.GoodsMng);
        }

        private void UserMngToolStripMenuItem1_Click(object sender, EventArgs e)
        {
			//ShowChildForm(typeof(UserMngForm).ToString());
			m_BaseForm.SwitchUserControl(HotelManagement.Common.OperationType.UserMng);
        }

        private void tsmiSearchTrade_Click(object sender, EventArgs e)
        {
			//ShowChildForm(typeof(SearchTradeForm).ToString());
			m_BaseForm.SwitchUserControl(HotelManagement.Common.OperationType.TradeSrch);
        }

		private void MainForm_Load(object sender, EventArgs e)
		{			
			m_BaseForm.MdiParent = this;

			m_BaseForm.WindowState = FormWindowState.Maximized;
			m_BaseForm.SwitchUserControl(HotelManagement.Common.OperationType.None);
			m_BaseForm.Show();
			m_BaseForm.Focus();			
		}
    }
}
